package com.jingtai.platform.service.shop.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.JsonObject;
import com.jingtai.common.enums.DeleteFlagEnum;
import com.jingtai.common.enums.EnableStatusEnum;
import com.jingtai.common.enums.InputEnum;
import com.jingtai.common.enums.LevelEnum;
import com.jingtai.common.template.ResultMapInfo;
import com.jingtai.core.entity.shop.GoodsCategory;
import com.jingtai.core.entity.shop.GoodsSpecifications;
import com.jingtai.core.entity.shop.GoodsSpecificationsValue;
import com.jingtai.core.entity.shop.MerchantsGoods;
import com.jingtai.core.entity.supply.SupplyGoods;
import com.jingtai.core.mapper.shop.GoodsSpecificationsMapper;
import com.jingtai.platform.service.shop.GoodsCategoryService;
import com.jingtai.platform.service.shop.GoodsSpecificationsService;
import com.jingtai.platform.service.shop.GoodsSpecificationsValueService;
import com.jingtai.platform.service.shop.MerchantsGoodsService;
import com.jingtai.platform.service.supply.SupplyGoodsService;
import com.jingtai.platform.util.ResultUtil;
import com.jingtai.platform.util.UrlToMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 商品规格表 impl
 *
 * @author sunlaiqian
 * @date 2020-12-09 15:39:05
 */
@Service
public class GoodsSpecificationsServiceImpl extends ServiceImpl<GoodsSpecificationsMapper, GoodsSpecifications> implements GoodsSpecificationsService {

    @Autowired
    private GoodsSpecificationsService goodsSpecificationsService;
    @Autowired
    private SupplyGoodsService supplyGoodsService;
    @Autowired
    private MerchantsGoodsService merchantsGoodsService;
    @Autowired
    private GoodsCategoryService goodsCategoryService;
    @Autowired
    private GoodsSpecificationsValueService goodsSpecificationsValueService;

    @Override
    public ResultUtil GoodsSpecificationslist(Integer page, Integer limit, String conditions){
        QueryWrapper<GoodsSpecifications> wrapper = new QueryWrapper<>();
        Map<String, String> map = UrlToMap.paramToMap(conditions);
        for(Map.Entry<String, String> entry : map.entrySet()){
            wrapper.like(entry.getKey(), entry.getValue());
        }
        wrapper.eq("delete_flag", DeleteFlagEnum.NOT_DELETE.getCode());
        return new ResultUtil(ResultMapInfo.GETSUCCESS,goodsSpecificationsService.page(new Page<>(page,limit),wrapper));
    }

    @Override
    public ResultUtil addGoodsSpecifications(GoodsSpecifications goodsSpecifications) {
        boolean save = goodsSpecificationsService.save(goodsSpecifications);
        if (save){
            return new ResultUtil(ResultMapInfo.ADDSUCCESS,goodsSpecifications.getId());
        }
        return new ResultUtil(ResultMapInfo.ADDFAIL);
    }

    @Override
    public ResultUtil getGoodsSpecificationsById(Long id) {
        GoodsSpecifications goodsSpecifications = goodsSpecificationsService.getById(id);
        return goodsSpecifications == null ?  new ResultUtil(ResultMapInfo.GETFAIL) : new ResultUtil(ResultMapInfo.GETSUCCESS,goodsSpecifications);
    }

    @Override
    public ResultUtil delGoodsSpecificationsById(Long id) {
        GoodsSpecifications goodsSpecifications = new GoodsSpecifications();
        goodsSpecifications.setId(id);
        goodsSpecifications.setDeleteFlag(DeleteFlagEnum.DELETE.getCode());
        boolean delState = goodsSpecificationsService.updateById(goodsSpecifications);
        return delState ?  new ResultUtil(ResultMapInfo.DELETESUCCESS) : new ResultUtil(ResultMapInfo.DELETEFAIL);
    }

    @Override
    public ResultUtil selectGoodsSpecificationsOfOne() {
        List<GoodsSpecifications> list = goodsSpecificationsService.list(Wrappers.lambdaQuery(GoodsSpecifications.class).eq(GoodsSpecifications::getLevel, LevelEnum.ONELEVEL.getValue()).eq(GoodsSpecifications::getEnableState, EnableStatusEnum.NORMAL.getValue()).eq(GoodsSpecifications::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
        return new ResultUtil(ResultMapInfo.GETSUCCESS,list);
    }

    @Override
    public ResultUtil selectGoodsSpecificationsOfAll() {
        //先查询一级分类
        List<GoodsSpecifications> list = goodsSpecificationsService.list(Wrappers.lambdaQuery(GoodsSpecifications.class).eq(GoodsSpecifications::getLevel, LevelEnum.ONELEVEL.getValue()).eq(GoodsSpecifications::getEnableState, EnableStatusEnum.NORMAL.getValue()).eq(GoodsSpecifications::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
        //根据一级分类查询二级分类
        List<JSONObject> jsonObjectList = new ArrayList<>();
        for (GoodsSpecifications goodsSpecifications : list){
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(goodsSpecifications));
            List<GoodsSpecifications> list2 = goodsSpecificationsService.list(Wrappers.lambdaQuery(GoodsSpecifications.class).eq(GoodsSpecifications::getLevelOfOne,goodsSpecifications.getId()).eq(GoodsSpecifications::getLevel, LevelEnum.TWOLEVEL.getValue()).eq(GoodsSpecifications::getEnableState, EnableStatusEnum.NORMAL.getValue()).eq(GoodsSpecifications::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
            jsonObject.put("list",list2);
            jsonObjectList.add(jsonObject);
        }
        return new ResultUtil(ResultMapInfo.GETSUCCESS,jsonObjectList);
    }

    @Override
    public ResultUtil selectGoodsSpecificationsByGoodsCode(String goodsCode,Integer type) {
        //1.根据商品编号查询商品
        Long categoryId = 0L;
        if (type == 0){
            SupplyGoods goods = supplyGoodsService.getOne(Wrappers.lambdaQuery(SupplyGoods.class).eq(SupplyGoods::getGoodsCode, goodsCode));
            categoryId = goods.getCategoryId();
        }else {
            MerchantsGoods merchantsGoods = merchantsGoodsService.getOne(Wrappers.lambdaQuery(MerchantsGoods.class).eq(MerchantsGoods::getGoodsCode, goodsCode));
            categoryId = merchantsGoods.getCategoryId();
        }

        //2.根据商品分类ID查询商品分类
        GoodsCategory goodsCategory = goodsCategoryService.getOne(Wrappers.lambdaQuery(GoodsCategory.class).eq(GoodsCategory::getId,categoryId));
        //3.根据商品分类查询规格
        List<GoodsSpecifications> list = goodsSpecificationsService.list(Wrappers.lambdaQuery(GoodsSpecifications.class).in(GoodsSpecifications::getId, goodsCategory.getSpecificationsId().split(",")).eq(GoodsSpecifications::getLevel,LevelEnum.TWOLEVEL.getValue()).orderByDesc(GoodsSpecifications::getWeight));
        //4.查询
        List<JSONObject> jsonObjectList = new ArrayList<>();
        for (GoodsSpecifications goodsSpecifications : list){
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(goodsSpecifications));
            if (goodsSpecifications.getType() == InputEnum.ONEINPUT.getValue()){
                List<GoodsSpecificationsValue> list1 = goodsSpecificationsValueService.list(Wrappers.lambdaQuery(GoodsSpecificationsValue.class).eq(GoodsSpecificationsValue::getSpecificationsId, goodsSpecifications.getId()).orderByDesc(GoodsSpecificationsValue::getWeight));
                jsonObject.put("list",list1);
            }
            jsonObjectList.add(jsonObject);
        }
        return new ResultUtil(ResultMapInfo.GETSUCCESS,jsonObjectList);
    }

    @Override
    public String formatJson(String json,String goodsCode,Integer type) {
        //1.根据商品编号查询商品
        Long categoryId = 0L;
        if (type == 0){
            SupplyGoods supplyGoods = supplyGoodsService.getOne(Wrappers.lambdaQuery(SupplyGoods.class).eq(SupplyGoods::getGoodsCode, goodsCode));
            categoryId = supplyGoods.getCategoryId();
        }else {
            MerchantsGoods merchantsGoods = merchantsGoodsService.getOne(Wrappers.lambdaQuery(MerchantsGoods.class).eq(MerchantsGoods::getGoodsCode, goodsCode));
            categoryId = merchantsGoods.getCategoryId();
        }

        //2.查询商品分类
        GoodsCategory goodsCategory = goodsCategoryService.getById(categoryId);
        //3.格式化json
        String[] split = json.split(",");
        Map<String,String> map = new HashMap<>();
        for (String str : split){
            String[] split1 = str.split(":");
            map.put(split1[0],split1[1]);
        }
        //4.查询所有一级菜单
        List<GoodsSpecifications> goodsSpecificationsList = goodsSpecificationsService.list(Wrappers.lambdaQuery(GoodsSpecifications.class).eq(GoodsSpecifications::getLevel,LevelEnum.ONELEVEL.getValue()).in(GoodsSpecifications::getId,goodsCategory.getSpecificationsId().split(",")).eq(GoodsSpecifications::getDeleteFlag,DeleteFlagEnum.NOT_DELETE.getCode()));
        //5.根据一级菜单查询二级菜单
        JSONObject jsonObject = new JSONObject();
        for (GoodsSpecifications goodsSpecifications : goodsSpecificationsList){
            List<GoodsSpecifications> goodsSpecifications1 = goodsSpecificationsService.list(Wrappers.lambdaQuery(GoodsSpecifications.class).eq(GoodsSpecifications::getLevelOfOne, goodsSpecifications.getId()).eq(GoodsSpecifications::getLevel, LevelEnum.TWOLEVEL.getValue()).in(GoodsSpecifications::getId, goodsCategory.getSpecificationsId().split(",")).eq(GoodsSpecifications::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
            JSONObject jsonObject1 = new JSONObject();
            for (GoodsSpecifications goodsSpecifications2 : goodsSpecifications1){
                if (map.containsKey(goodsSpecifications2.getSpecificationsName())){
                    jsonObject1.put(goodsSpecifications2.getSpecificationsName(),map.get(goodsSpecifications2.getSpecificationsName()));
                }
            }
            System.out.println(JSON.toJSONString(jsonObject1));
            jsonObject.put(goodsSpecifications.getSpecificationsName(),JSON.toJSONString(jsonObject1));
        }
        return JSON.toJSONString(jsonObject);
    }

    @Override
    public ResultUtil editGoodsSpecifications(GoodsSpecifications goodsSpecifications) {
        boolean editState = goodsSpecificationsService.updateById(goodsSpecifications);
        return editState ?  new ResultUtil(ResultMapInfo.EDITSUCCESS) : new ResultUtil(ResultMapInfo.EDITFAIL);
    }

}
